<?php defined('_JEXEC') or die('Restricted access'); ?>
<?php
$makepay->text          = "PAGE_MAKE_ADHOC_PAY";
$makepay->link          = "index.php?option=com_articlecashrewards&controller=awards&task=adhoc";
$pages[]                = $makepay;

$listadhoc->text        = "PAGE_LIST_ADHOC_PAYMENTS";
$listadhoc->link        = "index.php?option=com_articlecashrewards&controller=awards&task=list";
$pages[]                = $listadhoc;

$managetypes->text      = "PAGE_MANAGE_AWARD_TYPES";
$managetypes->link      = "index.php?option=com_articlecashrewards&controller=awards&task=manage";
$pages[]                = $managetypes;

//$paymentsmade->text     = "PAGE_PAYEMENTS_MADE";
//$paymentsmade->link     = "index.php?option=com_articlecashrewards&controller=reports&task=pendingpayments";
//$reportpages[]          = $paymentsmade;
?>
<form action="index.php?option=com_articlecashrewards" method="post" name="adminForm">
<?php defined('_JEXEC') or die('Restricted access'); ?>
<script language='javascript'>

function ajxGetUsers(){
    getXhr = function () {
            var xhr = null;

            if (window.XMLHttpRequest) {
                    xhr = new XMLHttpRequest();
            } else if (window.createRequest) {
                    xhr = window.createRequest();
            } else if (window.ActiveXObject) {
                    try {
                            xhr = new ActiveXObject('Msxml2.XMLHTTP');
                    } catch (e) {
                            try {
                                    xhr = new ActiveXObject('Microsoft.XMLHTTP');
                            } catch (e) {}
                    }
            }

            return xhr;
    };

    var keyword = document.getElementById('keyword').value;
    if(null == keyword || keyword.length < 1){
        alert('<?php echo JText::_("JS_ALERT_NO_VALUE")?>');
        return false;
    }
    var xhr = getXhr();
    
    xhr.onreadystatechange = function(){
        if(xhr.readyState == 4 && xhr.status == 200){
            var usertable=document.getElementById('ut_body');

            try {//Internet Explorer
                xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async="false";
                xmlDoc.loadXML(xhr.responseText);
            }
            catch(e) {
                try {//Firefox, Mozilla, Opera, etc.
                    parser=new DOMParser();
                    xmlDoc=parser.parseFromString(xhr.responseText,"text/xml");
                }
                catch(e) {alert(e.message)}
            }
            var users =xmlDoc.getElementsByTagName('items').item(0);
            if(users.childNodes.length == 0){
                usertable.innerHTML='<?php echo JText::_("MSG_NO_RESULTS");?>';
                document.getElementById('loadingImg').style.visibility = 'hidden';
                return true;
            }else{
                usertable.innerHTML='';
            }
            var k = 0;
            for (i=0; i < users.childNodes.length; i++){
                
                var myoption=users.childNodes[i];
                
                var newtr=document.createElement("tr");
                var td1 = document.createElement("td");
                var td2 = document.createElement("td");
                var td3 = document.createElement("td");
                var link = document.createElement("a");
                
                var items=myoption.childNodes[0].nodeValue.split('|');
                
                newtr.setAttribute("class", "row" + k)
                newtr.setAttribute("id", "row" + items[0]);

                td1.appendChild(document.createTextNode(items[0]));

                link.setAttribute("href", "#");
                link.setAttribute("onclick", "addUser(\"" + items[0] + "\")");
                link.innerHTML=items[1];
                td2.appendChild(link);

                td3.appendChild(document.createTextNode(items[2]));
                
                newtr.appendChild(td1);
                newtr.appendChild(td2);
                newtr.appendChild(td3);

                usertable.appendChild(newtr);
                
                k = 1 - k;
            }
            document.getElementById('loadingImg').style.visibility = 'hidden';
        }
    }
    document.getElementById('loadingImg').style.visibility = 'visible';
    xhr.open("GET","index2.php?option=com_articlecashrewards&controller=awards&task=getusers&keyword="+keyword,true);
    xhr.send(null);
}
function addUser(id){
    inputAuthors = document.getElementById("author_ids");
    if(inputAuthors.value == '' || inputAuthors.value == null){
        inputAuthors.value = id;
    }else{
        if(inputAuthors.value.indexOf(id) >= 0){
            alert('<?php echo JText::_("JS_ALERT_DUPLICATE");?>');
            return false;
        }
        inputAuthors.value = inputAuthors.value + ',' + id;
    }
    var usertable=document.getElementById('ut_body');
    var tr = usertable.getElementById("row" + id);
    if(null != tr){
        usertable.removeChild(tr);
    }
}
</script>
<table class="adminlist" cellspacing="1">
    <tr>
        <td valign="top" width="20%">
            <table width="100%" cellpadding="0" cellspacing="0">
                <tr>
                    <td align="right">
                        <div style="float: left;"><h2><?php echo JText::_("CPANEL_ADHOC")?></h2></div>
                    </td>
                    <td>
                        <div style="float: right;"><img src="components/com_articlecashrewards/assets/images/icon-48-acr-48.png"></div>
                    </td>
                </tr>
                <tr>
                    <td width="100%" colspan="2">
                        <div class="acrsubmenu">
                        <?php
                            foreach($pages as $page){?>
                                <a href="<?php echo $page->link ?>" class="acrlinkblock">
                                    <?php echo JText::_($page->text); ?>
                                </a><?php
                            }
                        ?>
                        </div>
                    </td>
                </tr>
            </table>
        </td>
        <td valign="top">
            <table class="admintable" width="100%">
                <tr>
                    <td width="100%">
                        <fieldset class="adminform">
                        <legend><?php echo JText::_( 'PAGE_MAKE_ADHOC_PAY' ); ?></legend>
                        <table class="admintable" cellspacing="1">
                            <tr>
                                <td class="key">
                                    <span class="editlinktip hasTip" title="">
                                        <?php echo JText::_('PAYMENT_AMOUNT'); ?>
                                    </span>
                                </td>
                                <td>
                                    <input class="inputbox" type="text" name="amount" id="amount" size="25"
                                        value="<?php if($this->payment){echo $this->payment['amount'];} ?>" />
                                </td>
                            </tr>
                            <tr>
                                <td class="key">
                                    <span class="editlinktip hasTip" title="">
                                        <?php echo JText::_('LABEL_PAYMENT_TYPE'); ?>
                                    </span>
                                </td>
                                <td>
                                    <select name="adhoc_type" id="adhoc_type">

                                        <?php
                                            if($this->pay_types){
                                                foreach($this->pay_types as $adhoc_type){
                                        ?>
                                                    <option value="<?php echo $adhoc_type->id; ?>"
                                                        <?php if($this->payment && ($this->payment['type'] == $adhoc_type->id)){echo "selected";}?>>
                                                        <?php echo $adhoc_type->description?>
                                        <?php
                                                }
                                            }
                                        ?>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td class="key">
                                    <span class="editlinktip hasTip" title="">
                                        <?php echo JText::_('LABEL_DESCRIPTION'); ?>
                                    </span>
                                </td>
                                <td>
                                    <input type="text" name="description" id="description" size="25"
                                        value="<?php if($this->payment){echo $this->payment['description'];} ?>">
                                </td>
                            </tr>
                            <tr>
                                <td class="key">
                                    <span class="editlinktip hasTip" title="<?php echo JText::_('ADHOC_PAYMENT_TO_DESC'); ?>">
                                        <?php echo JText::_('ADHOC_PAYMENT_TO'); ?>
                                    </span>
                                </td>
                                <td>
                                    <input class="inputbox" type="text" name="author_ids" id="author_ids" size="25"
                                        <?php if(isset($this->payment)){echo "readonly";}?>
                                        value="<?php if($this->payment){echo $this->payment['author'];} ?>" />
                                </td>
                            </tr>
                        </table>
                        </fieldset>
                    </td>
                </tr>
                <tr>
                    <td>
                        <?php if(!($this->payment)){ ?>
                        <fieldset class="adminform">
                        <legend><?php echo JText::_( 'ADHOC_FIND_AUTHOR' ); ?></legend>
                        <table width="100%" class="admintable">
                            <tr>
                                <td colspan="2">
                                    <table class="admintable">
                                        <tr>
                                            <td>
                                                <input type="text" name="keyword" id="keyword">
                                                <input type="button" onclick="javascript: return ajxGetUsers();" value="<?php echo JText::_('ADHOC_FIND_AUTHOR'); ?>" style="button">
                                                <div id="loadingImg" style="visibility: hidden; position: absolute; float: left"><?php echo JText::_('MSG_LOADING');?></div>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <table id="usertable" class="adminlist">
                                        <thead>
                                            <tr>
                                                <th class="title">
                                                    <?php echo JText::_('TITLE_USER_ID'); ?>
                                                </th>
                                                <th class="title">
                                                    <?php echo JText::_('TITLE_USER_NAME'); ?>
                                                </th>
                                                <th class="title">
                                                    <?php echo JText::_('TITLE_NAME'); ?>
                                                </th>
                                            </tr>
                                        </thead>
                                        <tbody id="ut_body">
                                        </tbody>
                                    </table>
                                </td>
                            </tr>
                        </table>
                        </fieldset>
                        <?php } ?>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<input type="hidden" name="option" value="com_articlecashrewards" />
<input type="hidden" name="controller" value="awards" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="payment_id" value="<?php if($this->payment){echo $this->payment['id'];} ?>" />
<?php echo JHTML::_( 'form.token' ); ?>
</form>
